using Holoville.HOTween;
using UnityEngine;

public class HelicopterBehavior : EntityBehavior
{
	public Vector3 lastPositionForDistance = Vector3.zero;

	public TriggerOnGround triggerOnGroundSript;

	public GameObject AimObject;

	private Tweener animationPosition;

	private Tweener animationRotation;

	private Vector3 lastPosition;

	private Vector3 lastRotation;

	public HeliController heliContollerScript;

	public override void Awake()
	{
		base.Awake();
		heliContollerScript = base.gameObject.GetComponent<HeliController>();
		heliContollerScript.StopRotors();
	}

	public override void Start()
	{
		base.Start();
		HOTween.Init();
		Disable();
	}

	public override void OnEnable()
	{
		base.OnEnable();
		lastPositionForDistance = base.transform.localPosition;
		if (carWithWeapon)
		{
			iCanShoot = true;
			if (FlyWindow.instance != null)
			{
				FlyWindow.instance.countHelicopterBullets = base.countBulletCar;
			}
		}
		StopAnimation();
		if (!isFree)
		{
			StartAnimationPropeler();
		}
	}

	private void ShowAim()
	{
		if (AimObject == null)
		{
			return;
		}
		if (!isFree && carWithWeapon && base.isMine)
		{
			Ray ray = new Ray(objForDirectionShoot.transform.position, objForDirectionShoot.transform.forward);
			RaycastHit hitInfo;
			bool flag = Physics.Raycast(ray, out hitInfo, 200f, collisionLayer);
			Debug.DrawRay(objForDirectionShoot.transform.localPosition, objForDirectionShoot.transform.forward);
			if (flag)
			{
				if (!AimObject.activeSelf)
				{
					AimObject.SetActive(true);
				}
				Quaternion rotation = Quaternion.FromToRotation(Vector3.back, hitInfo.normal);
				AimObject.transform.rotation = rotation;
				AimObject.transform.position = hitInfo.point - AimObject.transform.forward * 0.5f;
				AimObject.transform.localScale = Vector3.one * Mathf.Max(30f * hitInfo.distance / 150f, 4f);
			}
			else if (AimObject.activeSelf)
			{
				AimObject.SetActive(false);
			}
		}
		else if (AimObject.activeSelf)
		{
			AimObject.SetActive(false);
		}
	}

	private void LateUpdate()
	{
		if (!base.isMine && !isFree && base.photonView != null && ownerPhotonView != null && base.photonView.ownerId != ownerPhotonView.ownerId)
		{
			GetControll(ownerViewId);
		}
		inWater = triggerOnGroundSript.inWater;
		if (inWater)
		{
			heliContollerScript.CurEngineForce = 0f;
		}
		if (base.isMine && rigidCar.isKinematic)
		{
			rigidCar.isKinematic = false;
		}
		else if (!base.isMine && !rigidCar.isKinematic)
		{
			rigidCar.isKinematic = true;
		}
		if (settings.offlineMode || (!isFree && base.isMine))
		{
			if (lastPositionForDistance != Vector3.zero)
			{
				GameController.instance.playerScript.traveledDistanceInAir += Vector3.Distance(base.transform.localPosition, lastPositionForDistance);
			}
			if (GameController.instance.playerScript.traveledDistanceInAir >= 500f)
			{
				AllUnitaryAchievements.instance.TravelInAir_50000.Increment((int)GameController.instance.playerScript.traveledDistanceInAir);
				GameController.instance.playerScript.traveledDistanceInAir = 0f;
			}
			lastPositionForDistance = base.transform.localPosition;
		}
		ShowAim();
	}

	[PunRPC]
	public override void Kill(int iKillerId = -9999)
	{
		if (!isDead)
		{
			base.Kill(iKillerId);
			StopAnimationPropeler();
			rigidCar.useGravity = true;
			rigidCar.isKinematic = false;
			rigidCar.mass = 6000f;
		}
	}

	private void StopAnimation()
	{
		if (animationPosition != null)
		{
			HOTween.Kill(animationPosition);
		}
		if (animationRotation != null)
		{
			HOTween.Kill(animationRotation);
		}
		animationPosition = null;
		animationRotation = null;
	}

	private void AnimateInAir()
	{
		lastPosition = base.transform.localPosition;
		lastRotation = base.transform.localRotation.eulerAngles;
		if (animationPosition == null)
		{
			AnimatePosition();
		}
		if (animationRotation == null)
		{
			AnimateRotation();
		}
	}

	private void AnimatePosition()
	{
		animationPosition = HOTween.To(base.transform, Random.Range(2f, 2.3f), new TweenParms().Prop("localPosition", lastPosition + new Vector3(Random.Range(-0.1f, 0.1f), Random.Range(-0.3f, 0.3f), Random.Range(-0.1f, 0.1f))).UpdateType(UpdateType.TimeScaleIndependentUpdate).Ease(EaseType.Linear)
			.OnComplete(AnimatePosition));
	}

	private void AnimateRotation()
	{
		animationRotation = HOTween.To(base.transform, Random.Range(2f, 2.3f), new TweenParms().Prop("localRotation", lastRotation + new Vector3(Random.Range(-1f, 1f), Random.Range(-0.3f, 0.3f), Random.Range(-5f, 5f))).UpdateType(UpdateType.TimeScaleIndependentUpdate).Ease(EaseType.Linear)
			.OnComplete(AnimateRotation));
	}

	public override Transform GetIn(Humanoid player, int n = -1)
	{
		Transform @in = base.GetIn(player, n);
		setIconMiniMap(true);
		heliContollerScript.enabled = true;
		Enable();
		GetControll(player.viewId);
		if (base.isMine)
		{
			FlyWindow.instance.SetWeaponVisible(carWithWeapon);
			UpdateAmo();
			GameController.instance.CheckInterfaceHelicopter(true);
		}
		return @in;
	}

	public override Transform GetOut(Humanoid player, bool withAnim)
	{
		if (carWithWeapon)
		{
			setCountBullets(base.countBulletCar);
		}
		if (this == PlayerBehavior.MyPlayer.currentCar)
		{
			GameController.instance.CheckInterfaceHelicopter(false);
		}
		if (!triggerOnGroundSript.isOnGround)
		{
			AnimateInAir();
		}
		heliContollerScript.enabled = false;
		setIconMiniMap(false);
		Disable();
		return base.GetOut(player, withAnim);
	}

	public override void Enable()
	{
		isStopDrive = false;
		base.Enable();
		StopAnimation();
		StartAnimationPropeler();
		if ((settings.offlineMode || base.photonView.isMine) && GetComponent<carDamageEnemy>() == null)
		{
			base.gameObject.AddComponent<carDamageEnemy>();
		}
	}

	[PunRPC]
	public override void reset()
	{
		base.reset();
		StopAnimationPropeler();
		StopAnimation();
	}

	public override void Disable()
	{
		base.Disable();
		isStopDrive = true;
		if (carWithWeapon && AimObject.activeSelf)
		{
			AimObject.SetActive(false);
		}
		if (triggerOnGroundSript.isOnGround)
		{
			StopAnimationPropeler();
		}
		if (base.isMine)
		{
			Object.Destroy(base.gameObject.GetComponent<carDamageEnemy>());
		}
		rigidCar.mass = 60f;
	}

	public void StartAnimationPropeler()
	{
		StartAnimationPropelerOnline();
	}

	[PunRPC]
	private void StartAnimationPropelerOnline()
	{
		heliContollerScript.StartRotors();
	}

	public void StopAnimationPropeler()
	{
		StopAnimationPropelerOnline();
	}

	[PunRPC]
	private void StopAnimationPropelerOnline()
	{
		StopAnimation();
		heliContollerScript.StopRotors();
	}

	public override float GetCurrentSpeed()
	{
		return heliContollerScript.Acceleration;
	}

	protected override void UpdateAmo()
	{
		base.UpdateAmo();
		if (base.isMine && FlyWindow.instance != null)
		{
			FlyWindow.instance.countHelicopterBullets = base.countBulletCar;
		}
	}

	public override bool setIconMiniMap(bool val)
	{
		if (val && GameController.instance.playerScript != null && GameController.instance.playerScript.currentCar == this)
		{
			iconMiniMap.enabled = false;
			return false;
		}
		iconMiniMap.enabled = true;
		if (val)
		{
			iconMiniMap.SetType("helicopterWithPlayer");
		}
		else
		{
			iconMiniMap.SetType("helicopter");
		}
		return val;
	}
}
